<script setup>
import { ref, reactive, onBeforeMount } from "vue";
import useSpecsStore from "../../store/specsStore";
import Dialog from "./dialog.vue";

const specsStore = useSpecsStore();

onBeforeMount(() => {
  // 获取商品规格数据
  specsStore.getSpecsAction();
  // 获取商品规格总数
  specsStore.getSpecsTotalAction();
});

const dialog = ref(null);

// 新增
function addSpecs() {
  // 打开对话框
  specsStore.openDialog();
}
// 编辑
function editSpecs(specs) {
  console.log(specs, "编辑");

  // dialog.value.cateForm.pid = cate.pid
  // 打开对话框
  specsStore.openDialog();
}
// 删除
function confirmEvent(id) {
  // console.log(id,'删除')
}
</script>

<template>
  <el-card>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商城管理</el-breadcrumb-item>
      <el-breadcrumb-item>规格管理</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="right">
      <el-button @click="addSpecs" type="primary">新增商品规格</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="specsStore.specsList" style="width: 100%" max-height="400">
      <el-table-column prop="id" label="ID" width="80" fixed></el-table-column>
      <el-table-column
        prop="space_name"
        label="规格名称"
        width="120"
      ></el-table-column>
      <el-table-column prop="goods_id" label="关联商品" min-width="150">
      </el-table-column>
      <el-table-column prop="state" label="状态" min-width="100">
        <template v-slot="props">
          <el-tag type="success" size="small" v-if="props.row.status === 1"
            >启用</el-tag
          >
          <el-tag type="danger" size="small" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" min-width="150">
        <template v-slot="prop">
          <el-button
            @click="editSpecs(prop.row)"
            type="primary"
            size="small"
            round
            >编辑</el-button
          >
          <!-- 气泡确认框 -->
          <el-popconfirm
            width="160px"
            title="你确定要删除吗？"
            @confirm="confirmEvent(prop.row.id)"
          >
            <template #reference>
              <el-button type="danger" size="small" round>删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="specsStore.total"
      :current-page="specsStore.page"
      :page-size="specsStore.size"
      :page-sizes="[1, 5, 10, 20, 50, 100]"
      @size-change="changeSize"
      @current-change="changePage"
    />
  </el-card>
  <!-- 对话框 -->
  <Dialog ref="dialog" />
</template>

<style lang="scss" scoped>
.el-tag {
  margin: 5px;
}
.el-pagination {
  margin: 20px auto;
}
</style>
